package tack15;

import tack11.SuperArray;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private JLabel accountJLabel = new JLabel("转账账号：");
    private JLabel transferJLable= new JLabel("转账金额：");
    private JTextField accountField = new JTextField();
    private JPasswordField transferField = new JPasswordField();



    private ImageIcon sureImg= new ImageIcon("atmlogo/buttonSure.jpg");
    private ImageIcon clearImg= new ImageIcon("atmlogo/buttonOff.jpg");



    private Font font = new Font("黑体",Font.BOLD,30);

    private JButton sureBth= new JButton(sureImg);
    private JButton clearBth= new JButton(clearImg);



    public TransferPanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);
        sureBth.setBounds(530,370,140,50);
        clearBth.setBounds(360,370,150,50);
        clearBth.addActionListener(this);

        accountJLabel.setBounds(200,200,250,50);
        accountJLabel.setFont(font);

        accountField.setBounds(400,200,250,50);
        accountField.setFont(font);


        transferJLable.setBounds(200,300,250,50);
        transferJLable.setFont(font);

        transferField.setBounds(400,300,250,50);
        transferField.setFont(font);

        this.add(accountField);
        this.add(accountJLabel);
        this.add(transferField);
        this.add(transferJLable);
        this.add(sureBth);
        this.add(clearBth);
        this.setOpaque(false);
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBth) {
            SuperArray users = atmFrame.getUsers();
            User user = atmFrame.getLoginUser();
            if (accountField.equals(user.getAccount())) {
                JOptionPane.showMessageDialog(this, "不可以转给自己");
                return;
            }
            for (int i = 0; i < users.size(); i++) {
                User otherUser = (User) users.get(i);
                if (!otherUser.getAccount().equals(accountField.getText())) {
                    if (Double.parseDouble(transferField.getText()) < user.getBlance()) {
                        user.setBlance(user.getBlance() - Double.parseDouble(transferField.getText()));

                        //加
                        otherUser.setBlance(Double.parseDouble(transferField.getText()) + otherUser.getBlance());

                        JOptionPane.showMessageDialog(this, "转账成功");
                        return;
                    } else {
                        JOptionPane.showMessageDialog(this, "余额不足");
                        return;
                    }
                }
                JOptionPane.showMessageDialog(this, "抱歉你输入的账号错误,请重新输入");
                return;
            }
        }
        if (e.getSource() == clearBth) {
            atmFrame.switchPanel("login");
        }
    }
}
